package com.qeeyou.apps.android.craftsman.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
import android.text.TextUtils;

import com.qeeyou.apps.android.craftsman.po.LoginInfo;

public class SystemContext {

    private static SystemContext instance;
    private SharedPreferences preferences;                //偏好设置
    private Context context;

    private LoginInfo loginInfo;
    private String userId;
    private String token;
    private String username;
    private String password;


    public static SystemContext getInstance() {
        if (instance == null) {
            instance = new SystemContext();
        }
        return instance;
    }

    private SystemContext() {

    }

    //SystemContext初始化
    public void init(Context appContext) {
        this.context = appContext;
        this.preferences = appContext.getSharedPreferences("preferences", PreferenceActivity.MODE_PRIVATE);
    }

    //获取上下文
    public Context getContext() {
        return context;
    }

    //获取偏好设置
    public SharedPreferences getPreferences() {
        return this.preferences;
    }

    public String getToken() {
        this.token = this.preferences.getString("token", null);
        return token;
    }

    public void setToken(String token) {
        SharedPreferences.Editor editor = this.preferences.edit();
        if (token != null) {
            editor.putString("token", token);
            editor.commit();
            this.token = this.preferences.getString("token", null);
        } else {
            editor.remove("token");
            editor.commit();
            this.token = null;
        }
    }

    public String getUserId() {
        this.userId = this.preferences.getString("userId", null);
        return userId;
    }

    public void setUserId(String userId) {
        SharedPreferences.Editor editor = this.preferences.edit();
        if (userId != null) {
            editor.putString("userId", userId);
            editor.commit();
            this.userId = this.preferences.getString("userId", null);
        } else {
            editor.remove("userId");
            editor.commit();
            this.userId = null;
        }
    }

    public String getUsername() {
        this.username = this.preferences.getString("username", null);
        return username;
    }

    public void setUsername(String username) {
        SharedPreferences.Editor editor = this.preferences.edit();
        if (username != null) {
            editor.putString("username", username);
            editor.commit();
            this.username = this.preferences.getString("username", null);
        } else {
            editor.remove("username");
            editor.commit();
            this.username = null;
        }
    }

    public String getPassword() {
        this.password = this.preferences.getString("password", null);
        return password;
    }

    public void setPassword(String password) {
        SharedPreferences.Editor editor = this.preferences.edit();
        if (password != null) {
            editor.putString("password", password);
            editor.commit();
            this.password = this.preferences.getString("password", null);
        } else {
            editor.remove("password");
            editor.commit();
            this.password = null;
        }
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(getToken());
    }

    public LoginInfo getLoginInfo() {
        LoginInfo loginInfo = new LoginInfo();
        String userid = this.preferences.getString("userId", null);
        loginInfo.setUserid(userid);
        String token = this.preferences.getString("token", null);
        loginInfo.setToken(token);
        String name = this.preferences.getString("name", null);
        loginInfo.setName(name);
        String phone = this.preferences.getString("phone", null);
        loginInfo.setPhone(phone);
        String age = this.preferences.getString("age", null);
        loginInfo.setAge(age);
        String sex = this.preferences.getString("sex", null);
        loginInfo.setSex(sex);
        String address = this.preferences.getString("address", null);
        loginInfo.setAddress(address);
        String avator = this.preferences.getString("avator", null);
        loginInfo.setAvator(avator);
        String idcard = this.preferences.getString("idcard", null);
        loginInfo.setIdcard(idcard);
        return loginInfo;
    }

    public void setLoginInfo(LoginInfo loginInfo) {
        SharedPreferences.Editor editor = this.preferences.edit();
        if(loginInfo != null) {
            if(loginInfo.getUserid() != null) {
                editor.putString("userId", loginInfo.getUserid());
            } else {
                editor.remove("userId");
            }
            if(loginInfo.getToken() != null) {
                editor.putString("token", loginInfo.getToken());
            } else {
                editor.remove("token");
            }
            if(loginInfo.getUsername() != null) {
                editor.putString("username", loginInfo.getUsername());
            } else {
                editor.remove("username");
            }
            if(loginInfo.getName() != null) {
                editor.putString("name", loginInfo.getName());
            } else {
                editor.remove("name");
            }
            if(loginInfo.getPhone() != null) {
                editor.putString("phone", loginInfo.getPhone());
            } else {
                editor.remove("phone");
            }
            if(loginInfo.getSex() != null) {
                editor.putString("sex", loginInfo.getSex());
            } else {
                editor.remove("sex");
            }
            if(loginInfo.getAge() != null) {
                editor.putString("age", loginInfo.getAge());
            } else {
                editor.remove("age");
            }
            if(loginInfo.getAddress() != null) {
                editor.putString("address", loginInfo.getAddress());
            } else {
                editor.remove("address");
            }
            if(loginInfo.getAvator() != null) {
                editor.putString("avator", loginInfo.getAvator());
            } else {
                editor.remove("avator");
            }
            if(loginInfo.getIdcard() != null) {
                editor.putString("idcard", loginInfo.getIdcard());
            } else {
                editor.remove("idcard");
            }
        } else {
            editor.remove("userId");
            editor.remove("token");
            editor.remove("username");
            editor.remove("name");
            editor.remove("phone");
            editor.remove("sex");
            editor.remove("age");
            editor.remove("idcard");
            editor.remove("address");
            editor.remove("avator");
        }
        editor.commit();
        this.loginInfo = loginInfo;
    }

}
